package com.xiaobaibai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaobaibai.common.IndexEnum;
import com.xiaobaibai.dao.IndexProductDao;
import com.xiaobaibai.entity.IndexProduct;
import com.xiaobaibai.service.IIndexProductService;
import io.swagger.models.auth.In;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Indexed;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaobaibai
 * @since 2020-10-16
 */
@Service
public class IndexProductServiceImpl extends ServiceImpl<IndexProductDao, IndexProduct> implements IIndexProductService {

    @Cacheable(value = "indexProduct",key = "#root.methodName")
    @Override
    public Map<String, List<IndexProduct>> getIndexProductMap() {

        QueryWrapper<IndexProduct> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","product_id","type","product_name","product_image","product_unit","product_old_price","product_price");
        List<IndexProduct> list = this.list(queryWrapper);

        Map<String,List<IndexProduct>> map=new HashMap<>();
        List<IndexProduct> xinpin=new ArrayList<>();
        List<IndexProduct> rexiao=new ArrayList<>();

        list.forEach(l->{
            if(l.getType().equals(IndexEnum.XINPIN.getCode())){
                xinpin.add(l);
            }else if(l.getType().equals(IndexEnum.REXIAO.getCode())){
                rexiao.add(l);
            }
        });

        map.put(IndexEnum.XINPIN.getName(),xinpin);
        map.put(IndexEnum.REXIAO.getName(),rexiao);

        return map;
    }

    @Cacheable(value = "indexProduct",key = "#root.methodName")
    @Override
    public List<IndexProduct> getIndexProduct() {
        QueryWrapper<IndexProduct> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","product_id","type","product_name","product_image","product_unit","product_old_price","product_price");
        queryWrapper.eq("type",4);
        return this.list(queryWrapper);
    }

}
